package com.seele.tigerwallet.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.AppCompatImageView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.seele.tigerwallet.DemoFragment;
import com.seele.tigerwallet.R;
import com.seele.tigerwallet.async.Log;
import com.seele.tigerwallet.jpush.JPushService;
import com.seele.tigerwallet.ui.base.BaseActivity;
import com.seele.tigerwallet.ui.fragment.AssetsFragment;
import com.seele.tigerwallet.ui.fragment.AssetsFragment2;
import com.seele.tigerwallet.ui.fragment.FoundFrgament;
import com.seele.tigerwallet.ui.fragment.MarketFragment;
import com.seele.tigerwallet.ui.fragment.MyFragment;
import com.seele.tigerwallet.ui.utils.StatusBarUtil;
import com.seele.tigerwallet.ui.utils.StatusBarUtils;
import com.seele.tigerwallet.ui.views.LinearLinearLayout;
import com.seele.tigerwallet.ui.views.NavigationChildView;
import com.seele.tigerwallet.views.bottombar.BottomBar;
import com.seele.tigerwallet.views.bottombar.BottomBarTab;
import com.seele.tigerwallet.views.bottombar.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;


/**
 * 主界面
 */
public class MainActivity extends BaseActivity {

    private List<NavigationChildView> list = new ArrayList<>();//装所有控件的list
    private List<BottomBarTab> bottomBarTabs = new ArrayList<>();//装所有控件的list

    private int[] tabNames = new int[]{R.string.main_assets, R.string.main_market, R.string.main_found, R.string.main_my};

//    private int[] tabIcons = {R.drawable.assets_selector, R.drawable.market_selector, R.drawable.found_selector, R.drawable.my_selector};

    private int[] tabNormalIcons = {R.drawable.assets_normal, R.drawable.market_normal, R.drawable.found_normal, R.drawable.my_normal};

    private int[] tabSelectIcons = {R.drawable.assets_select, R.drawable.market_select, R.drawable.found_select, R.drawable.my_select};

    private List<Fragment> fragments;

    private AssetsFragment mAssetsFragment;
    private MarketFragment mMarketFragment;
    private FoundFrgament mFoundFrgament;
    private MyFragment mMyFragment;
    private FragmentManager mFragmentManager;

    private  DemoFragment mDemoFragment;

    private long mExitTime;
    private LinearLinearLayout mLinearLinearLayout;

    private BottomBar mBottomBar;



    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    protected void init() {
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        if (null!=savedInstanceState){
            mFragmentManager=getSupportFragmentManager();
            mFragmentManager.popBackStackImmediate(null, 1);//弹出所有fragment
        }

        if (null==mFragmentManager){
            mFragmentManager=getSupportFragmentManager();
            Log.e("savedInstanceState","21111111111111111111111111111");
        }
        initView();
        initFragment();
        setStatusBar();



    }

    private void setStatusBar() {
        StatusBarUtils.with(this).init();
    }


    public void setWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            StatusBarUtil.setStatusBarLightMode(getWindow());
        }
    }


    private void initView() {
        mLinearLinearLayout = findViewById(R.id.navigation_container_ll);
        //初始化BottomBarTab
        initBottomBarTab();

        mBottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(int tabId) {
                resetBottomBarTab();
                switch (tabId) {
                    case R.id.assets_nv:
                        StatusBarUtils.with(MainActivity.this).init();
                        clickTab(0, mAssetsFragment);
                        break;
                    case R.id.market_nv:
                        initWidow();
                        clickTab(1, mMarketFragment);
                        break;
                    case R.id.found_nv:
                        initWidow();
                        clickTab(2, mFoundFrgament);
                        break;
                    case R.id.my_nv:
                        initWidow();
                        clickTab(3, mMyFragment);
                        break;
                }

            }
        }, false);

        resetBottomBarTab();
        //设置初始化Item
        setPosition(0);


    }

    /**
     * 复位
     */
    private void resetBottomBarTab() {
        if (null != bottomBarTabs && bottomBarTabs.size() > 0) {
            for (int i = 0; i < bottomBarTabs.size(); i++) {
                BottomBarTab bottomBarTab = bottomBarTabs.get(i);
                AppCompatImageView imageView = bottomBarTab.getIconView();
                TextView textView = bottomBarTab.getTitleView();
                imageView.setImageResource(tabNormalIcons[i]);
                textView.setText(tabNames[i]);
                textView.setTextColor(getResources().getColor(R.color.color_A5A5A5));
            }
        }
    }

    private void initBottomBarTab() {
        mBottomBar = findViewById(R.id.bottomBar);
        int cout = mBottomBar.getTabCount();
        for (int i = 0; i < cout; i++) {
            BottomBarTab bottomBarTab = mBottomBar.getTabAtPosition(i);
            bottomBarTabs.add(bottomBarTab);
        }
    }


    private void setPosition(int position) {
        if (null != bottomBarTabs && bottomBarTabs.size() > 0) {
            BottomBarTab bottomBarTab = bottomBarTabs.get(position);
            AppCompatImageView imageView = bottomBarTab.getIconView();
            TextView textView = bottomBarTab.getTitleView();
            imageView.setImageResource(tabSelectIcons[position]);
            textView.setText(tabNames[position]);
            textView.setTextColor(getResources().getColor(R.color.color_313A4C));
        }
    }

    /**
     * 初始化Fragment
     */

    private AssetsFragment2 assetsFragment2;
    private void initFragment() {
//        if (null==mDemoFragment){
//            mDemoFragment=new DemoFragment();
//        }
        if (null == mAssetsFragment) {
            mAssetsFragment = AssetsFragment.newInstanec();
        }
        if (null == mMarketFragment) {
            mMarketFragment = MarketFragment.newInstanec();
        }
        if (null == mFoundFrgament) {
            mFoundFrgament = FoundFrgament.newInstanec();
        }
        if (null == mMyFragment) {
            mMyFragment = MyFragment.newInstanec();
        }
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        if (!mAssetsFragment.isAdded()) {
            fragmentTransaction.add(R.id.main_container, mAssetsFragment, AssetsFragment.class.getSimpleName());
            fragmentTransaction.hide(mAssetsFragment);
        }
        if (!mMarketFragment.isAdded()) {
            fragmentTransaction.add(R.id.main_container, mMarketFragment);
            fragmentTransaction.hide(mMarketFragment);
        }
        if (!mFoundFrgament.isAdded()) {
            fragmentTransaction.add(R.id.main_container, mFoundFrgament);
            fragmentTransaction.hide(mFoundFrgament);
        }
        if (!mMyFragment.isAdded()) {
            fragmentTransaction.add(R.id.main_container, mMyFragment);
            fragmentTransaction.hide(mMyFragment);
        }
        hideAllFragment(fragmentTransaction);
        // 默认显示第一个fragment
        fragmentTransaction.show(mAssetsFragment);
        fragmentTransaction.commit();

    }

    /**
     * 隐藏所有fragment
     *
     * @param fragmentTransaction
     */
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        fragmentTransaction.hide(mAssetsFragment);
        fragmentTransaction.hide(mMarketFragment);
        fragmentTransaction.hide(mFoundFrgament);
        fragmentTransaction.hide(mAssetsFragment);
    }

    /**
     * 点击下面的Tab按钮
     *
     * @param tabFragment
     */
    private void clickTab(int position, Fragment tabFragment) {

        // 清除上次选中状态
        resetBottomBarTab();


        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

        // 隐藏所有fragment
        hideAllFragment(fragmentTransaction);

        // 显示该Fragment
        fragmentTransaction.show(tabFragment);

        // 提交事务
        fragmentTransaction.commit();

        // 改变tab的样式,设置为选中状态
        setPosition(position);

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(MainActivity.this, getResources().getString(R.string.system_exit), Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }


}
